package com.iot08.admin.service;

import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.admin.utils.ExcelUtil;
import com.iot08.common.config.redis.RedisService;
import com.iot08.common.constant.Const;
import com.iot08.common.entity.admin.ChannelArea;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.CanCheckPosition;
import com.iot08.common.entity.app.Device;
import com.iot08.common.entity.app.HeartRateRecord;
import com.iot08.common.response.Response;
import com.iot08.common.service.base.BaseService;
import com.iot08.common.utils.CollectionUtil;
import com.iot08.common.utils.RegexUtil;
import com.iot08.common.utils.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @version 1.0
 * @Description:
 * @time: 2021/08/24 16:582020/10/17 8:44
 * @author: WeiChao
 */
@Service
public class HealthMService extends BaseService<HeartRateRecord> {

    @Resource
    private DeviceService deviceService;
    @Resource
    private ChannelAreaService channelAreaService;
    @Resource
    private OperationService operationService;
    @Resource
    private RedisService redisService;

//    /**
//     * 根据 User 和输入的imei号 获取设备信息
//     * @param user 当前用户
//     * @param imei 输入的imei
//     * @time: 2021/08/24 16:58
//     * @author: WeiChao
//     * @version 1.0
//     */
//    public Device findWithImei(SysUser user, String imei) {
//
//        Criteria criteria = new Criteria();
//        Integer userAreaLevel = 0;
//        if (user.getType() == Const.CHANNELUSER_TYPE) {
//            userAreaLevel = user.getChannelArea() == null ? 1 : user.getChannelArea().getLevel();
//            if (userAreaLevel == 1) {
//                criteria.andOperator(Criteria.where("agent_code").in(CollectionUtil.longToStr(user.getChannelAff().getChannelCodes())));
//            } else {
//                List<String> channelIdList = channelAreaService.listIdById(user.getChannelAreaId(), true);
//                criteria.andOperator(Criteria.where("channel_area_id").in(channelIdList));
//            }
//        }
//
//        // _id 为 imei 的
//        Query query = new Query().addCriteria(Criteria.where("_id").is(imei));
//        Device oneDev = mongoTemplate.findOne(query, Device.class);
//        if(oneDev == null){
//           //设备号 不存在
//            return;
//        }
//        List<Long> channelCodes = user.getChannelAff().getChannelCodes();
//        if (!channelCodes.contains(oneDev.getAgent())){
//            //设备号存在，但无权限访问该设备
//        }
//
//
//
//
//
//        if (user.getType() == Const.CHANNELUSER_TYPE) {
//            criteria.orOperator(Criteria.where("agent").in(user.getChannelAff().getChannelCodes()),
//                    Criteria.where("agent").exists(false));
//        }
//
//
//
//        for (Device heartRate : list) {
//            Device device = deviceService.findById(heartRate.getImei());
//            if (device != null && device.getNowloc() != null && device.getNowloc().get("timestamp") instanceof Double) {
//                device.getNowloc().put("timestamp", ((Double) device.getNowloc().get("timestamp")).longValue());
//            }
//
//        }
//        Page<HeartRateRecord> result = new PageImpl<HeartRateRecord>(list, pageable, count);
//        return result;
//    }
}
